﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TopBuilds.Restaurant.Data;
using TopBuilds.Restaurant.ViewModel;

namespace TopBuilds.Restaurant.ManagementClient
{
    /// <summary>
    /// MemberManagement.xaml 的交互逻辑
    /// </summary>
    public partial class MemberManagement : Page
    {
        public MemberManagement()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            #region member变量
            string id          = textBox1.Text;
            string name        = textBox2.Text;
            int sex            = Convert.ToInt32(textBox3.Text);
            string password    = textBox4.Text;
            int discountId     = Convert.ToInt32(comboBox1.Text);
            int consumption    = Convert.ToInt32(textBox6.Text);
            string mobile      = textBox7.Text;
            string phone       = textBox8.Text;
            string company     = textBox9.Text;
            string address     = textBox10.Text;
            int state          = Convert.ToInt32(textBox11.Text);
            int consumeCount   = Convert.ToInt32(textBox12.Text);
            #endregion

            if (id != null && id.Length > 0)
            {
                Member member = new Member(Convert.ToInt32(id), name, sex, password, discountId, consumption, mobile, phone, company, address, state, consumeCount);
                if (MemberObject.Add(member) == -1)
                {
                    MessageBox.Show("添加失败");
                    return;
                }

                #region re-binding
                Binding bindingId = new Binding("Id");
                bindingId.Mode = BindingMode.OneWay;
                textBox1.SetBinding(TextBox.TextProperty, bindingId);

                Binding bindingName = new Binding("Name");
                bindingName.Mode = BindingMode.OneWay;
                textBox2.SetBinding(TextBox.TextProperty, bindingName);

                Binding bindingSex = new Binding("Sex");
                bindingSex.Mode = BindingMode.OneWay;
                textBox3.SetBinding(TextBox.TextProperty, bindingSex);

                Binding bindingPassword = new Binding("Password");
                bindingPassword.Mode = BindingMode.OneWay;
                textBox4.SetBinding(TextBox.TextProperty, bindingPassword);

                Binding bindingDiscountOptionId = new Binding("DiscountOptionId");
                bindingDiscountOptionId.Mode = BindingMode.OneWay;
                comboBox1.SetBinding(TextBox.TextProperty, bindingDiscountOptionId);

                Binding bindingConsumption = new Binding("Consumption");
                bindingConsumption.Mode = BindingMode.OneWay;
                textBox6.SetBinding(TextBox.TextProperty, bindingConsumption);

                Binding bindingMobile = new Binding("Mobile");
                bindingMobile.Mode = BindingMode.OneWay;
                textBox7.SetBinding(TextBox.TextProperty, bindingMobile);

                Binding bindingPhone = new Binding("Phone");
                bindingPhone.Mode = BindingMode.OneWay;
                textBox8.SetBinding(TextBox.TextProperty, bindingPhone);

                Binding bindingCompany = new Binding("Company");
                bindingCompany.Mode = BindingMode.OneWay;
                textBox9.SetBinding(TextBox.TextProperty, bindingCompany);

                Binding bindingAddress = new Binding("Address");
                bindingAddress.Mode = BindingMode.OneWay;
                textBox10.SetBinding(TextBox.TextProperty, bindingAddress);

                Binding bindingState = new Binding("State");
                bindingState.Mode = BindingMode.OneWay;
                textBox11.SetBinding(TextBox.TextProperty, bindingState);

                Binding bindingConsumeCount = new Binding("ConsumeCount");
                bindingConsumeCount.Mode = BindingMode.OneWay;
                textBox12.SetBinding(TextBox.TextProperty, bindingConsumeCount);

                Binding bindingRegDate = new Binding("RegDate");
                bindingRegDate.Mode = BindingMode.OneWay;
                textBox13.SetBinding(TextBox.TextProperty, bindingRegDate);

                Binding bindingLastConsumeDate = new Binding("LastConsumeDate");
                bindingLastConsumeDate.Mode = BindingMode.OneWay;
                textBox14.SetBinding(TextBox.TextProperty, bindingLastConsumeDate);

                #endregion

                MemberCollection mc = listBox1.ItemsSource as MemberCollection;
                mc.Add(member);
                MessageBox.Show("添加成功");
            }
        }

        private void buttonUpdate_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = listBox1.SelectedIndex;

            if (selectedIndex >= 0)
            {
                MemberCollection m = listBox1.ItemsSource as MemberCollection;

                Member member = m[selectedIndex] as Member;

                member.Id             = Convert.ToInt32(textBox1.Text);
                member.Name           = textBox2.Text;
                member.Sex            = Convert.ToInt32(textBox3.Text);
                member.Password       = textBox4.Text;
                member.DiscountOptionId     = Convert.ToInt32(comboBox1.Text);
                member.Consumption    = Convert.ToInt32(textBox6.Text);
                member.Mobile         = textBox7.Text;
                member.Phone          = textBox8.Text;
                member.Company        = textBox9.Text;
                member.Address        = textBox10.Text;
                member.State          = Convert.ToInt32(textBox11.Text);
                member.ConsumeCount   = Convert.ToInt32(textBox12.Text);

                if (MemberObject.Update(member) == -1)
                {
                    MessageBox.Show("更新失败");
                }
                else
                    MessageBox.Show("更新成功");

                #region re-binding

                Binding bindingId = new Binding("Id");
                bindingId.Mode = BindingMode.OneWay;
                textBox1.SetBinding(TextBox.TextProperty, bindingId);

                Binding bindingName = new Binding("Name");
                bindingName.Mode = BindingMode.OneWay;
                textBox2.SetBinding(TextBox.TextProperty, bindingName);

                Binding bindingSex = new Binding("Sex");
                bindingSex.Mode = BindingMode.OneWay;
                textBox3.SetBinding(TextBox.TextProperty, bindingSex);

                Binding bindingPassword = new Binding("Password");
                bindingPassword.Mode = BindingMode.OneWay;
                textBox4.SetBinding(TextBox.TextProperty, bindingPassword);

                Binding bindingDiscountOptionId = new Binding("DiscountOptionId");
                bindingDiscountOptionId.Mode = BindingMode.OneWay;
                comboBox1.SetBinding(TextBox.TextProperty, bindingDiscountOptionId);

                Binding bindingConsumption = new Binding("Consumption");
                bindingConsumption.Mode = BindingMode.OneWay;
                textBox6.SetBinding(TextBox.TextProperty, bindingConsumption);

                Binding bindingMobile = new Binding("Mobile");
                bindingMobile.Mode = BindingMode.OneWay;
                textBox7.SetBinding(TextBox.TextProperty, bindingMobile);

                Binding bindingPhone = new Binding("Phone");
                bindingPhone.Mode = BindingMode.OneWay;
                textBox8.SetBinding(TextBox.TextProperty, bindingPhone);

                Binding bindingCompany = new Binding("Company");
                bindingCompany.Mode = BindingMode.OneWay;
                textBox9.SetBinding(TextBox.TextProperty, bindingCompany);

                Binding bindingAddress = new Binding("Address");
                bindingAddress.Mode = BindingMode.OneWay;
                textBox10.SetBinding(TextBox.TextProperty, bindingAddress);

                Binding bindingState = new Binding("State");
                bindingState.Mode = BindingMode.OneWay;
                textBox11.SetBinding(TextBox.TextProperty, bindingState);

                Binding bindingConsumeCount = new Binding("ConsumeCount");
                bindingConsumeCount.Mode = BindingMode.OneWay;
                textBox12.SetBinding(TextBox.TextProperty, bindingConsumeCount);

                Binding bindingRegDate = new Binding("RegDate");
                bindingRegDate.Mode = BindingMode.OneWay;
                textBox13.SetBinding(TextBox.TextProperty, bindingRegDate);

                Binding bindingLastConsumeDate = new Binding("LastConsumeDate");
                bindingLastConsumeDate.Mode = BindingMode.OneWay;
                textBox14.SetBinding(TextBox.TextProperty, bindingLastConsumeDate);

                #endregion

                listBox1.ItemsSource = null;
                listBox1.ItemsSource = m;

            }
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = listBox1.SelectedIndex;
            if (selectedIndex >= 0)
            {
                MemberCollection m             = listBox1.ItemsSource as MemberCollection;
                int memid = (m[selectedIndex] as Member).Id;

                if (MessageBoxResult.Yes == MessageBox.Show("确定要删除么?", "警告", MessageBoxButton.YesNo))
                {
                    m.RemoveAt(selectedIndex);

                    #region re-binding

                    Binding bindingId = new Binding("Id");
                    bindingId.Mode = BindingMode.OneWay;
                    textBox1.SetBinding(TextBox.TextProperty, bindingId);

                    Binding bindingName = new Binding("Name");
                    bindingName.Mode = BindingMode.OneWay;
                    textBox2.SetBinding(TextBox.TextProperty, bindingName);

                    Binding bindingSex = new Binding("Sex");
                    bindingSex.Mode = BindingMode.OneWay;
                    textBox3.SetBinding(TextBox.TextProperty, bindingSex);

                    Binding bindingPassword = new Binding("Password");
                    bindingPassword.Mode = BindingMode.OneWay;
                    textBox4.SetBinding(TextBox.TextProperty, bindingPassword);

                    Binding bindingDiscountOptionId = new Binding("DiscountOptionId");
                    bindingDiscountOptionId.Mode = BindingMode.OneWay;
                    comboBox1.SetBinding(TextBox.TextProperty, bindingDiscountOptionId);

                    Binding bindingConsumption = new Binding("Consumption");
                    bindingConsumption.Mode = BindingMode.OneWay;
                    textBox6.SetBinding(TextBox.TextProperty, bindingConsumption);

                    Binding bindingMobile = new Binding("Mobile");
                    bindingMobile.Mode = BindingMode.OneWay;
                    textBox1.SetBinding(TextBox.TextProperty, bindingMobile);

                    Binding bindingPhone = new Binding("Phone");
                    bindingPhone.Mode = BindingMode.OneWay;
                    textBox7.SetBinding(TextBox.TextProperty, bindingPhone);

                    Binding bindingCompany = new Binding("Company");
                    bindingCompany.Mode = BindingMode.OneWay;
                    textBox8.SetBinding(TextBox.TextProperty, bindingCompany);

                    Binding bindingAddress = new Binding("Address");
                    bindingAddress.Mode = BindingMode.OneWay;
                    textBox9.SetBinding(TextBox.TextProperty, bindingAddress);

                    Binding bindingState = new Binding("State");
                    bindingState.Mode = BindingMode.OneWay;
                    textBox10.SetBinding(TextBox.TextProperty, bindingState);

                    Binding bindingConsumeCount = new Binding("ConsumeCount");
                    bindingConsumeCount.Mode = BindingMode.OneWay;
                    textBox11.SetBinding(TextBox.TextProperty, bindingConsumeCount);

                    Binding bindingRegDate = new Binding("RegDate");
                    bindingRegDate.Mode = BindingMode.OneWay;
                    textBox12.SetBinding(TextBox.TextProperty, bindingRegDate);

                    Binding bindingLastConsumeDate = new Binding("LastConsumeDate");
                    bindingLastConsumeDate.Mode = BindingMode.OneWay;
                    textBox13.SetBinding(TextBox.TextProperty, bindingLastConsumeDate);
                    #endregion

                    if ( ! MemberObject.Delete(memid) )
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }
    }
}
